import { inlineSource as inline } from 'inline-source';
import JsReport from 'jsreport-core';
import JsreportHtml2docx from 'jsreport-html-embedded-in-docx';

/**
 * html转换为Word文档,注意生成的文件不能使用openoffice正常打开
 * @param html 需转换的内容
 * @param attachment 文件名称
 * @example html2docx(html,attachment);
 */
export default async function html2docx(html: string, attachment?: string, rootpath = '') {
	const jsreport = JsReport();
	jsreport.use(JsreportHtml2docx());

	const content = await inline(html, {
		compress: true,
		rootpath
	});
	await jsreport.init();
	const resp = await jsreport.render({
		template: {
			content,
			engine: 'none',
			recipe: 'html-embedded-in-docx'
		}
	});
	if (attachment) {
		return {
			attachment,
			buff: resp.content,
			content_type: 'application/vnd.openxmlformats-officedocument.wordprocessingml.document'
		};
	} else {
		return {
			buff: resp.content,
			content_type: 'application/vnd.openxmlformats-officedocument.wordprocessingml.document'
		};
	}
}
